define(function () {
    'use strict';

    function ctrl($scope, $filter, $ionicScrollDelegate, $stateParams, $timeout, InfoService,$ionicLoading,CommonService) {

        $scope.vm = {};
        $scope.channel = {};

        init();

        $scope.doRefresh = function() {
            init(true).finally(function(){
                $scope.$broadcast('scroll.refreshComplete');
            });
        };

        $scope.$watch("vm.activeTab", function(newValue, oldValue){
            if(newValue) {
                $timeout(function(){
                    $ionicScrollDelegate.resize();
                }, 500);
            }
        });

        function init(refresh){
            if(!$scope.channels){
                $ionicLoading.show();
            }
            $scope.channel.channelId = $stateParams['id'];
            if(refresh) {
                return InfoService.getChannelInfo($scope.vm.activeTab).then(function (data) {
                    for(var i = 0; i < $scope.channels.length; i++) {
                        if($scope.channels[i].id == data.id) {
                            $scope.channels[i] = data;
                            $scope.channels[i].page_no = 1;
                            $scope.channels[i].products = new Array();
                            break;
                        }
                    }
                    $scope.vm.loadMore = true;
                });
            } else {
                return InfoService.getChannelInfosByParentId($scope.channel.channelId).then(function(data){
                    $scope.channels = data;
                    for(var i = 0; i < $scope.channels.length; i++) {
                        var channel = $scope.channels[i];
                        if(i == 0 && !$scope.vm.activeTab) {
                            $scope.vm.activeTab=channel.id;
                        }
                        channel.page_no = 1;
                        channel.products = new Array();
                        $ionicLoading.hide();
                    }
                    $scope.vm.loadMore = true;
                });
            }
        }

        $scope.loadMore = function() {
            for(var i = 0; i < $scope.channels.length; i++) {
                var channel = $scope.channels[i];
                if($scope.vm.activeTab == channel.id) {
                    InfoService.getProductsByChannelId(channel.id, channel.page_no).then(
                        function(products) {
                            if(products && products.length > 0) {
                                for(var i=0;i<products.length;i++){
                                    var fund = products[i];
                                    fund.prod_min_subscribe = $filter('prodMinSubscribe')(CommonService.getMinAmount(fund));
                                }
                                channel.products = channel.products.concat(products);
                                channel.page_no ++;
                                $scope.vm.page_no = channel.page_no;
                            } else {
                                $scope.vm.loadMore = false;
                            }
                            $scope.$broadcast('scroll.infiniteScrollComplete');
                        }
                    );
                    return;
                }
            }

        };
    }

    ctrl.$inject = ['$scope','$filter','$ionicScrollDelegate','$stateParams','$timeout', 'InfoService','$ionicLoading','CommonService'];
    return ctrl;
});